package Presenter;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;

import Models.Supplies;

public class SuppliesPresenter {
	public ArrayList<Supplies> tryJSONParseArray(String input){
		ArrayList<Supplies> supplies = new ArrayList<Supplies>();
		try{
			JSONObject jsonObj = null;
			jsonObj = XML.toJSONObject(input);
			JSONObject jsonAllObjects = jsonObj.getJSONObject("suppliess");
				if(!jsonAllObjects.isNull("supplies")){
					Object item = jsonAllObjects.get("supplies");
					if(item instanceof JSONArray){
						JSONArray jArray = (JSONArray)item;
						for(int i=0; i < jArray.length(); i++){
							JSONObject jObject = jArray.getJSONObject(i);
							supplies.add(createSupplyFromJSONObject(jObject));
						}
					} else {
						JSONObject jObject = (JSONObject)item;
						supplies.add(createSupplyFromJSONObject(jObject));
					}
				}
			return supplies;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public Supplies tryJSONParse(String input){
		try{
			JSONObject jsonObj = null;
			jsonObj = XML.toJSONObject(input);
			JSONObject jsonHop = jsonObj.getJSONObject("supplies");
			Supplies supply = createSupplyFromJSONObject(jsonHop);
			return supply;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public Supplies createSupplyFromJSONObject(JSONObject jsonSupply){
		Supplies supply = new Supplies();
		try{
			supply.setLeadTime(jsonSupply.getInt("leadTime"));
			supply.setMinQty(jsonSupply.getDouble("minimumQty"));
			supply.setOnHand(jsonSupply.getDouble("supOnHand"));
			supply.setResaleable(jsonSupply.getBoolean("supResalable"));
			supply.setResalePrice(jsonSupply.getDouble("supReSalePrice"));
			supply.setSpokenFor(jsonSupply.getDouble("supSpoken"));
			supply.setSupplyID(jsonSupply.getInt("supID"));
			supply.setSupplyName(jsonSupply.getString("supName"));
			supply.setSupplyNotes(jsonSupply.getString("supNotes"));
			supply.setSupplyNumber(jsonSupply.getString("supNumber"));
			supply.setSupplyOnOrder(jsonSupply.getDouble("supOnOrder"));
			supply.setSupplyPrice(jsonSupply.getDouble("supPrice"));
			supply.setSupplyQtyPerCost(jsonSupply.getDouble("supQtyPer"));
			supply.setSupplyUnits(jsonSupply.getString("supUnits"));
			supply.setWearhouseID(jsonSupply.getInt("supWH"));
			supply.setWholesalePrice(jsonSupply.getDouble("supWholeSalePrice"));
			return supply;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(Supplies item){
		try{
			JSONObject json = new JSONObject();
			json.put("leadTime", item.getLeadTime());
			json.put("minimumQty", item.getMinQty());
			json.put("supOnHand", item.getOnHand());
			json.put("supResalable", item.isResaleable());
			json.put("supReSalePrice", item.getResalePrice());
			json.put("supSpoken", item.getSpokenFor());
			json.put("supID", item.getSupplyID());
			json.put("supName", item.getSupplyName());
			json.put("supNotes", item.getSupplyNotes());
			json.put("supNumber", item.getSupplyNumber());
			json.put("supOnOrder", item.getSupplyOnOrder());
			json.put("supPrice", item.getSupplyPrice());
			json.put("supQtyPer", item.getSupplyQtyPerCost());
			json.put("supUnits", item.getSupplyUnits());
			json.put("supWH", item.getWearhouseID());
			json.put("supWholeSalePrice", item.getWholesalePrice());
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
}
